﻿Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing.Design
Imports System.Security.Permissions
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Activities.Rules.Design
    <PermissionSet(SecurityAction.LinkDemand, Name:="FullTrust")> _
    Friend NotInheritable Class ConditionNameEditor
        Inherits UITypeEditor
        ' Methods
        Public Overrides Function EditValue(ByVal typeDescriptorContext As ITypeDescriptorContext, ByVal serviceProvider As IServiceProvider, ByVal o As Object) As Object
            If (typeDescriptorContext Is Nothing) Then
                Throw New ArgumentNullException("typeDescriptorContext")
            End If
            If (serviceProvider Is Nothing) Then
                Throw New ArgumentNullException("serviceProvider")
            End If
            Dim selectedName As Object = o
            Me.editorService = DirectCast(serviceProvider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
            If (Not Me.editorService Is Nothing) Then
                Dim component As Activity = Nothing
                Dim service As IReferenceService = [TryCast](serviceProvider.GetService(GetType(IReferenceService)),IReferenceService)
                If (Not service Is Nothing) Then
                    component = [TryCast](service.GetComponent(typeDescriptorContext.Instance),Activity)
                End If
                Dim name As String = [TryCast](typeDescriptorContext.PropertyDescriptor.GetValue(typeDescriptorContext.Instance),String)
                Dim dialog As New ConditionBrowserDialog(component, name)
                If (DialogResult.OK = Me.editorService.ShowDialog(dialog)) Then
                    selectedName = dialog.SelectedName
                End If
            End If
            Return selectedName
        End Function

        Public Overrides Function GetEditStyle(ByVal typeDescriptorContext As ITypeDescriptorContext) As UITypeEditorEditStyle
            Return UITypeEditorEditStyle.Modal
        End Function


        ' Fields
        Private editorService As IWindowsFormsEditorService
    End Class
End Namespace

